<template>
  <div v-loading="loading" style="width: 100%;height: 100%;">
    <template v-if="prams.type == 1">
      <pdf
      v-for="i in numPages"
      :key="i"
      :src="src"
      :page="i"
      style="display: block; width: 600px; margin:0 auto;"
    />
    </template>
    <template v-else>
      <div class="html" v-html="html"></div>
    </template>
    <div v-if="error" class="errtips">
      <i
        class="el-icon-warning-outline f30 text-999"
      />
      <p class="text-999 f16">简历生成失败，请联系管理员</p>
    </div>
  </div>
</template>

<script>
import pdf from 'vue-pdf'
import { getWordResume } from '@/api/common'
import { editState } from '@/api/personnel'
export default {
  components: { pdf },
  filters: {
  },
  props: {
    prams: {
      type: Object
    },
    order: {
      type: Number
    },
    dataId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      src: null,
      html: '',
      numPages: 0,
      loading: true,
      error: false
    }
  },
  mounted() {
    this.loadPdf()
  },
  methods: {
    loadPdf() {
      this.loading = true
      this.error = false
      getWordResume(this.prams).then(response => { 
        if(response.headers['content-type'] != 'application/pdf'){
           
        //  this.url = window.URL.createObjectURL(new Blob([response.data]))
        //  window.open(this.url)
        //  var encodedString = String.fromCharCode.apply(null, new Uint16Array(response.data))
        if(response.headers['content-type'].indexOf("application/json") != -1){
            this.error = true;
            this.$emit('popres', this.order)
        }else{
          let html = response.data;
          this.loading = false
          if(!html){
            this.error = true;
            this.$emit('popres', this.order)
          }else{
            this.html = html;
            if(this.dataId){
              editState(
                {
                  dataId: this.dataId,
                  state: 1
                }
              ).then(res => {
              })
            }
          }
        }
        }else{
          if (!response.data.byteLength) { this.loading = false; this.error = true; throw new Error('no data') }
        this.src = pdf.createLoadingTask(window.URL.createObjectURL(new Blob([response.data])))
        this.src.promise.then(pdf => {
          this.numPages = pdf.numPages
          this.loading = false
          // 修改简历为已阅读状态
          if(this.dataId){
            editState(
              {
                dataId: this.dataId,
                state: 1
              }
            ).then(res => {
            })
          }
          
        }).catch(err => {
          this.loading = false
          this.$message.error('简历生成失败，请联系管理员')
          this.error = true
          console.log(err)
        })
        }
        
      }).catch(err => {
        this.$emit('popres', this.order)
        if (err.message.includes('no data')) this.$emit('popres', this.order)
      })
    }
  }
}
</script>

<style lang="scss" scoped>
  .errtips{
    text-align: center;
    width: 100%;
    padding: 50px 0;
    .f30{
      font-size: 40px;
      color: #AAAAAA
    }
  }
</style>
